//*****************************************************************************
//
// screen_saver.c - A screen saver for the OSRAM OLED display.
//
// Copyright (c) 2006-2007 Luminary Micro, Inc.  All rights reserved.
// 
// Software License Agreement
// 
// Luminary Micro, Inc. (LMI) is supplying this software for use solely and
// exclusively on LMI's microcontroller products.
// 
// The software is owned by LMI and/or its suppliers, and is protected under
// applicable copyright laws.  All rights are reserved.  Any use in violation
// of the foregoing restrictions may subject the user to criminal sanctions
// under applicable laws, as well as to civil liability for the breach of the
// terms and conditions of this license.
// 
// THIS SOFTWARE IS PROVIDED "AS IS".  NO WARRANTIES, WHETHER EXPRESS, IMPLIED
// OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, IMPLIED WARRANTIES OF
// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE APPLY TO THIS SOFTWARE.
// LMI SHALL NOT, IN ANY CIRCUMSTANCES, BE LIABLE FOR SPECIAL, INCIDENTAL, OR
// CONSEQUENTIAL DAMAGES, FOR ANY REASON WHATSOEVER.
// 
// This is part of revision 1716 of the Stellaris Peripheral Driver Library.
//
//*****************************************************************************


#include "interrupt.h"
//#include "systick.h"
#include "adc.h"
#include "hw_memmap.h"
#include "hw_types.h"
#include "gpio.h"
#include "sysctl.h"
#include "timer.h"
#include "osram96x16x1.h"
#include "globals.h"
#include "random.h"
#include "screen_saver.h"
//#include "sysctl.h"

#include <stdio.h>

//*****************************************************************************


// structure of point 
// agrumets by X, Y and time

struct 
{
	long x ;// 0 -95
	long y ;// 0 - 15
	long time ; // 1 - 11
} point;


// generate random numbers for x coordinate( 0 - 95), for y coordinate (0 - 15), for time (1 - 11);


#ifdef DEBUG
void
__error__(char *pcFilename, unsigned long ulLine)
{
}
#endif


#ifdef DEBUG
void
__error__(char *pcFilename, unsigned long ulLine)
{
}
#endif

//*****************************************************************************
//
// The interrupt handler for the ADC interrupt.
//
//*****************************************************************************

#ifdef DEBUG
void
__error__(char *pcFilename, unsigned long ulLine)
{
}
#endif

//*****************************************************************************
//
// The interrupt handler for the ADC interrupt.
//
//*****************************************************************************
void
ADCIntHandler(void)
{
    unsigned long ulData;

    //
    // Clear the ADC interrupt.
    //
    ADCIntClear(ADC_BASE, 3);

    //
    // Read the data from the ADC.
    //
    ADCSequenceDataGet(ADC_BASE, 3, &ulData);

    //
    // Add the ADC data to the random number entropy pool.
    //
    RandomAddEntropy(ulData);

   
}



int main ()
{
 
 //	char num[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' };
	char text[14]={'0'};
	unsigned long randomNum;





SysCtlClockSet(SYSCTL_SYSDIV_10 | SYSCTL_USE_PLL | SYSCTL_OSC_MAIN |
                   SYSCTL_XTAL_6MHZ);

    //
    // Enable the peripherals used by the application.
    //
    SysCtlPeripheralEnable(SYSCTL_PERIPH_ADC);
    SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOA);
    SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOC);
    SysCtlPeripheralEnable(SYSCTL_PERIPH_TIMER0);
    SysCtlPeripheralEnable(SYSCTL_PERIPH_TIMER1);
    SysCtlPeripheralEnable(SYSCTL_PERIPH_UART0);

    //
    // Configure the ADC to sample the potentiometer when the timer expires.
    // After sampling, the ADC will interrupt the processor; this is used as
    // the heartbeat for the game.
    //
    ADCSequenceConfigure(ADC_BASE, 3, ADC_TRIGGER_TIMER, 0);
    ADCSequenceStepConfigure(ADC_BASE, 3, 0,
                             ADC_CTL_CH0 | ADC_CTL_IE | ADC_CTL_END);
    ADCSequenceEnable(ADC_BASE, 3);
    ADCIntEnable(ADC_BASE, 3);
	 
    //IntEnable(ADC_CTL_CH3);

	IntMasterEnable();

    //
    // Configure the first timer to generate a 10 kHz PWM signal for driving
    // the user LED.
    //
    TimerConfigure(TIMER0_BASE, TIMER_CFG_16_BIT_PAIR | TIMER_CFG_B_PWM);
    TimerLoadSet(TIMER0_BASE, TIMER_B, (SysCtlClockGet() / 10000) - 1);
    TimerMatchSet(TIMER0_BASE, TIMER_B, 0);
    TimerControlLevel(TIMER0_BASE, TIMER_B, true);
    TimerEnable(TIMER0_BASE, TIMER_B);

    //
    // Configure the second timer to generate triggers to the ADC to sample the
    // potentiometer.
    //

    TimerConfigure(TIMER1_BASE, TIMER_CFG_32_BIT_PER);
    TimerLoadSet(TIMER1_BASE, TIMER_A, SysCtlClockGet() / 120);
    TimerControlStall(TIMER1_BASE, TIMER_A, true);
    TimerControlTrigger(TIMER1_BASE, TIMER_A, true);
    TimerEnable(TIMER1_BASE, TIMER_A);

   
     
	randomNum=RandomNumber() % 96;
 
	sprintf(text, "%u", randomNum);
	OSRAM96x16x1StringDraw(text, 6, 0);
	randomNum=RandomNumber() % 16;
	sprintf(text, "%u", randomNum);
    OSRAM96x16x1StringDraw(text, 12, 1);
	randomNum=RandomNumber() % 11;
	randomNum+=1;
	sprintf(text, "%u", randomNum);
    OSRAM96x16x1StringDraw(text, 48, 1);


}



    




	
